package com.example.bookkeepingelf.business.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.bookkeepingelf.R;
import com.example.bookkeepingelf.base.utils.ResourceUtils;
import com.example.bookkeepingelf.business.database.entity.AccountBook;
import com.example.bookkeepingelf.business.listener.MyOnClickListener;

import java.util.List;

public class AccountBookManageListContentListViewAdapter extends ArrayAdapter<AccountBook> {

    private Context context;
    private List<AccountBook> item;

    private LayoutInflater layoutInflater;

    private MyOnClickListener<AccountBook> myOnClickListener;

    private int resourceId;


    public AccountBookManageListContentListViewAdapter(@NonNull Context context, MyOnClickListener<AccountBook> myOnClickListener, int resource, List<AccountBook> item) {
        super(context, resource, item);
        this.context = context;
        this.item = item;
        this.resourceId = resource;
        this.myOnClickListener = myOnClickListener;
        this.layoutInflater = LayoutInflater.from(context);

    }

    @SuppressLint("SetTextI18n")
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // 获取Logo对象
        AccountBook accountBook = item.get(position);
        // 加载布局
        View view = this.layoutInflater.inflate(resourceId, parent, false);
        ImageView iconImageView = view.findViewById(R.id.bkm_account_book_icon_image);
        TextView nameTextView = view.findViewById(R.id.bkm_account_book_name);
        TextView mainNameTextView = view.findViewById(R.id.bkm_account_book_main_ame);
        TextView amountTextView = view.findViewById(R.id.bkm_account_book_total_amount);
        ImageView menuButImage = view.findViewById(R.id.bkm_account_book_menu_but_image);

        iconImageView.setImageResource(ResourceUtils.getIconResourceId(context, accountBook.getIcon()));
        amountTextView.setText(accountBook.getAmount().toString());
        nameTextView.setText(accountBook.getAccountBookName());
        if (accountBook.getMain()) {
            nameTextView.setTextColor(context.getColor(R.color.mainGreen_700));
            mainNameTextView.setVisibility(View.VISIBLE);
        }

        menuButImage.setOnClickListener(v1 -> {
            PopupMenu popup = new PopupMenu(context, v1);
            // 加载菜单资源文件
            popup.getMenuInflater().inflate(R.menu.bkm_account_book_menu, popup.getMenu());

            // 设置菜单项点击事件
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    int itemId = item.getItemId();
                    if (itemId == R.id.menu_update) {
                        myOnClickListener.onMyClickUpdate(accountBook);
                        return true;

                    }else if (itemId == R.id.menu_delete) {
                        myOnClickListener.onMyClickDelete(accountBook);
                        return true;
                    }else if (itemId == R.id.menu_main) {
                        myOnClickListener.onMyClickChange(accountBook);
                        return true;
                    }
                    return false;
                }
            });
            // 显示PopupMenu
            popup.show();
        });

        return view;
    }
}
